
*this file constructs occupation x czone x year weights

clear


foreach year in 2007 2010 2011 2012 2013 2014 2015 2016 {

use statefip  labforce empstat year age classwkr occ2010 perwt    puma using "$dir/data/data_public/acs_bgyears.dta", clear
	
	// Prepare
	
	keep if age >=15 & age<65
	gen pop  = 1
	
	*labforce ==2 -> in the labor force
	
	gen lforce           =  (labforce == 2)
	
		*want to exclude self-employed CLASSWKR == 1
	
	gen emp = (empstat == 1) if classwkr != 1 
	
	
	*empstat==1 means employed
	
	
	*indicator for if you're employed
gen emp_ind=0
	replace emp_ind=1 if emp==1
	
	//Mapping PUMAs to Commuting Zones
	
if `year'< 2012 { //puma definitions changed in 2012

		gen     puma2000 = real(string(statefip) + "00" + string(puma)) if puma<1000 
		replace puma2000 = real(string(statefip) + "0" + string(puma)) if puma>=1000 & puma<10000
		replace puma2000 = real(string(statefip) + string(puma)) if puma>=10000
		

		joinby puma2000 using "$dir/data/interim/cw_cty_czone_puma2000_afact.dta"  , unmatched(master) 
				drop _merge
				
			
		*note, for new orleans, from 2007-2011, 3 pumas were collapsed into one, b/c of low pop
		*so 2277777 is the combination of  2201801,  2201802, and 2201905
		*however all 3 czones are completely contained within commuting zone 3300, so they each
		*have an afactor of 1
		
			replace czone = 3300 	if puma2000 == 2277777 
	replace afact = 1 if puma2000 == 2277777 
	
}

 if `year' >=2012 {

		gen     puma2012 = real(string(statefip) + "00" + string(puma)) if puma<1000 
		replace puma2012 = real(string(statefip) + "0" + string(puma)) if puma>=1000 & puma<10000
		replace puma2012 = real(string(statefip) + string(puma)) if puma>=10000
		
		joinby puma2012 using "$dir/data/interim/cw_cty_czone_puma2012_afact.dta"  , unmatched(master) 
				drop _merge
		
	

}	
				
	gen new_weight = perwt*afact
		
	
	gen occ2010_aug=occ2010 //create augmented occ variable that includes zero if non-employed
	replace occ2010_aug = 0 if emp_ind==0 
	
	collapse (sum) emp  (sum) lforce  [pw=new_weight], by(year occ2010_aug czone)

	by czone year, sort: egen emp_sum_czone = total(emp)
		
	gen oas = emp if (occ2010_aug >=5000 & occ2010_aug <6000)
	
	by czone year, sort: egen oas_sum_czone             = total(oas)


	keep if year == `year' & (oas!=. & oas !=0)
	

	 keep year occ2010_aug czone oas emp_sum_czone oas_sum_czone
	  

	gen oas_share = oas/emp_sum_czone // oas_sum gives the total employment in that OAS variable
	gen oas_share_oas = oas/oas_sum_czone
	
	
	rename occ2010 census2010
	
	keep year czone oas_share oas_share_oas census2010
	
	save "$dir/data/interim/oas_cz_weights_`year'", replace

}
	
